﻿// <copyright file="ColumnVisual.cs" company="Oleg V. Polikarpotchkin">
// Copyright © 2008-2009 Oleg V. Polikarpotchkin. All Right Reserved
// </copyright>
// <author>Oleg V. Polikarpotchkin</author>
// <email>ov-p@yandex.ru</email>
// <date>2009-03-10</date>
// <summary>OpenWPFChart library. Column Chart Item visual.</summary>
// <revision>$Id$</revision>

using System.Windows;
using System.Windows.Media;

namespace OpenWPFChart.Parts
{
	/// <summary>
	/// Column Chart Item visual class.
	/// </summary>
	/// <remarks>
	/// Draws the data passed in by Points property as columns.
	/// </remarks>
	public class ColumnVisual : ItemVisual
	{
		/// <summary>
		/// Renders the ColumnChartItem
		/// </summary>
		protected internal override void Render()
		{
			Children.Clear();

			using (DrawingContext dc = RenderOpen())
			{
				ColumnChartItemDataView dataView = DataView as ColumnChartItemDataView;
				if (dataView == null)
					return;
				IPoint2DSeries pointSeries = dataView.ItemData as IPoint2DSeries;
				if (pointSeries == null)
					return;

				ChartScale hScale = dataView.HorizontalScale, vScale = dataView.VerticalScale;
				if (hScale == null || !hScale.CompatibleWith(dataView.ItemData.GetDimensionType(0))
					|| vScale == null || !vScale.CompatibleWith(dataView.ItemData.GetDimensionType(1)))
					return;

				// Chart area size.
				Size areaSize = new Size(hScale.ToPixels(hScale.Stop), vScale.ToPixels(vScale.Stop));

				// Clipping region
				//RectangleGeometry clip;
				//if (itemDataView.Orientation == Orientation.Horizontal)
				//    clip = new RectangleGeometry(new Rect(0, 0
				//        , hScale.ToPixels(hScale.Stop), vScale.ToPixels(vScale.Stop)));
				//else
				//    clip = new RectangleGeometry(new Rect(0, 0
				//        , vScale.ToPixels(vScale.Stop), hScale.ToPixels(hScale.Stop)));
				//dc.PushClip(clip);

				// Loop by points.
				foreach (var pt in pointSeries.Points)
				{
					double x, y;
					bool isPtInsideArea;
					if (dataView.Orientation == Orientation.Horizontal)
					{
						x = hScale.ToPixels(pt.X);
						y = vScale.ToPixels(pt.Y);
						isPtInsideArea = isInsideArea(new Point(x, y), areaSize);
					}
					else // Orientation == Orientation.Vertical
					{
						y = hScale.ToPixels(pt.X);
						x = vScale.ToPixels(pt.Y);
						isPtInsideArea = isInsideArea(new Point(y, x), areaSize);
					}

					// Draw the column.
					StreamGeometry geometry = new StreamGeometry();
					using (StreamGeometryContext ctx = geometry.Open())
					{
						double xStart = x - dataView.ColumnWidth / 2;
						double xStop = xStart + dataView.ColumnWidth;
						ctx.BeginFigure(new Point(xStart, areaSize.Height)
							, true /* is filled */, true /* is closed */);
						ctx.PolyLineTo(new Point[] 
						{ 
							new Point(xStart, y), 
							new Point(xStop, y),
							new Point(xStop, areaSize.Height)
						}
							, false /* is stroked */, false /* is smooth join */);
					}
					geometry.Freeze();
					dc.DrawGeometry(dataView.ColumnBrush, null, geometry);
				}
			}
		}
	}
}
